package cn.congzhou.crm.web.controller;

import cn.congzhou.crm.domain.SystemDictionary;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.ISystemDictionaryService;
import cn.congzhou.crm.utils.ResultJSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    private Logger logger = LoggerFactory.getLogger(SystemDictionaryController.class);

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.selectByPage(qo);
        model.addAttribute("ro",pageInfo);

        return "/systemDictionary/list";
    }


    @RequestMapping("/input")
    @RequiresPermissions(value = {"编辑字典目录","systemDictionary:input"},logical = Logical.OR)
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", systemDictionaryService.selectByPrimaryKey(id));
        }

        return "/systemDictionary/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"保存/修改字典目录","systemDictionary:saveOrUpdate"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(SystemDictionary sysDic) {
        ResultJSON result = new ResultJSON();
        try {
            systemDictionaryService.saveOrUpdate(sysDic);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"删除字典目录","systemDictionary:delete"},logical = Logical.OR)
    @ResponseBody
    public Object delete(Long id) {
        ResultJSON result = new ResultJSON();
        try {
            systemDictionaryService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }

        return result;
    }

    //==========================添加ligerUI添加的方法============================================//

    @RequestMapping(value = "toSystemDictionaryGrid",method = RequestMethod.GET)
    @RequiresPermissions(value = {"查看字典目录","systemDictionary:query"},logical = Logical.OR)
    public String toSystemDictionaryGrid(){
        return "/systemDictionary/systemDictionaryGrid";
    }

    @ResponseBody
    @RequestMapping(value = "getSystemDictionaryData",method = RequestMethod.POST)
    @RequiresPermissions(value = {"查看字典目录","systemDictionary:query"},logical = Logical.OR)
    public Object getSystemDictionaryData(){
        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectAll();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",systemDictionaries);
        return map;
    }




}
